﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
//using Microsoft.Office.Server.UserProfiles;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;

namespace EFSPWebParts
{
    [ToolboxItemAttribute(false)]
    public class SiteDataQuery : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/EFSPWebParts/SiteDataQuery/SiteDataQueryUserControl.ascx";

        public enum enumSearchWebsScope
        {
            Recursive = 0,
            SiteCollection
        };

        public enum enumListBaseType
        {
            GenericList = 0,
            DocumentLibrary = 1,
            DiscussionForum = 3,
            VoteOrSurvey = 4,
            IssuesList = 5
        };

        private enumSearchWebsScope _WebsScope = enumSearchWebsScope.Recursive;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Webs Scope"),
        WebDisplayName("Webs Scope"),
        WebDescription("Webs Scope (Recursive or SiteCollection)")]
        public enumSearchWebsScope WebsScope
        {
            get {
                //if (string.IsNullOrEmpty(_WebsScope))
                //    _WebsScope = "Recursive";

                return _WebsScope;
            }
            set
            {
                _WebsScope = value;
            }
        }

        private enumListBaseType _ListBaseType = enumListBaseType.DocumentLibrary;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("List Base Type"),
        WebDisplayName("List Base Type"),
        WebDescription("List Base Type")]
        public enumListBaseType ListBaseType
        {
            get
            {
                //if (string.IsNullOrEmpty(_ListBaseType))
                //    _ListBaseType = "GenericList";

                return _ListBaseType;
            }
            set
            {
                _ListBaseType = value;
            }
        }


        private bool _UseAccessibleHeader = false;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Use Accessible Header"),
        WebDisplayName("Use Accessible Header"),
        WebDescription("Use Accessible Header")]
        public bool UseAccessibleHeader
        {
            get
            {
                return _UseAccessibleHeader;
            }
            set
            {
                _UseAccessibleHeader = value;
            }
        }

        private bool _AllowSorting = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Allow Sorting"),
        WebDisplayName("Allow Sorting"),
        WebDescription("Allow Sorting")]
        public bool AllowSorting
        {
            get
            {
                return _AllowSorting;
            }
            set
            {
                _AllowSorting = value;
            }
        }

        private bool _AllowFiltering = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Allow Filtering"),
        WebDisplayName("Allow Filtering"),
        WebDescription("Allow Filtering")]
        public bool AllowFiltering
        {
            get
            {
                return _AllowFiltering;
            }
            set
            {
                _AllowFiltering = value;
            }
        }

        private bool _AllowPaging = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Allow Paging"),
        WebDisplayName("Allow Paging"),
        WebDescription("Allow Paging")]
        public bool AllowPaging
        {
            get
            {
                return _AllowPaging;
            }
            set
            {
                _AllowPaging = value;
            }
        }

        private int _PageSize = 3;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Page Size"),
        WebDisplayName("Page Size"),
        WebDescription("Page Size")]
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
            }
        }

        private Color _RowStyle_BackColor = Color.White;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("RowStyle BackColor"),
        WebDisplayName("RowStyle BackColor"),
        WebDescription("RowStyle BackColor")]
        public Color RowStyle_BackColor
        {
            get
            {
                return _RowStyle_BackColor;
            }
            set
            {
                _RowStyle_BackColor = value;
            }
        }

        private Color _AlternatingRowStyle_BackColor = Color.White;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("AlternatingRowStyle BackColor"),
        WebDisplayName("AlternatingRowStyle BackColor"),
        WebDescription("AlternatingRowStyle BackColor")]
        public Color AlternatingRowStyle_BackColor
        {
            get
            {
                return _AlternatingRowStyle_BackColor;
            }
            set
            {
                _AlternatingRowStyle_BackColor = value;
            }
        }

        private uint _QueryRowLimit = uint.MaxValue;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("Query Row Limit"),
        WebDisplayName("Query Row Limit"),
        WebDescription("Query Row Limit (0 means not limit)")]
        public uint QueryRowLimit
        {
            get
            {
                if (_QueryRowLimit == uint.MaxValue)
                    _QueryRowLimit = 500;

                return _QueryRowLimit;
            }
            set
            {
                _QueryRowLimit = value;
            }
        }

        private string _CAMLQuery = string.Empty;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("CAML Query"),
        WebDisplayName("CAML Query"),
        WebDescription("CAML Query")]
        public string CAMLQuery
        {
            get { return _CAMLQuery; }
            set
            {
                _CAMLQuery = value;
            }
        }

        private string _CAMLQueryOrderBy = string.Empty;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("CAML Query Order By"),
        WebDisplayName("CAML Query Order By"),
        WebDescription("CAML Query Order By")]
        public string CAMLQueryOrderBy
        {
            get { return _CAMLQueryOrderBy; }
            set
            {
                _CAMLQueryOrderBy = value;
            }
        }

        private bool _ColumnNameFileName = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("File Name"),
        WebDisplayName("File Name"),
        WebDescription("File Name")]
        public bool ColumnNameFileName
        {
            get
            {
                return _ColumnNameFileName;
            }
            set
            {
                _ColumnNameFileName = value;
            }
        }

        private bool _ColumnNameFileNameFiltering = false;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("File Name Filtering"),
        WebDisplayName("File Name Filtering"),
        WebDescription("File Name Filtering")]
        public bool ColumnNameFileNameFiltering
        {
            get
            {
                return _ColumnNameFileNameFiltering;
            }
            set
            {
                _ColumnNameFileNameFiltering = value;
            }
        }

        private bool _ColumnNameModifiedBy = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("Modified By"),
        WebDisplayName("Modified By"),
        WebDescription("Modified By")]
        public bool ColumnNameModifiedBy
        {
            get
            {
                return _ColumnNameModifiedBy;
            }
            set
            {
                _ColumnNameModifiedBy = value;
            }
        }

        private bool _ColumnNameModifiedByFiltering = false;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("Modified By (Filtering)"),
        WebDisplayName("Modified By (Filtering)"),
        WebDescription("Modified By (Filtering)")]
        public bool ColumnNameModifiedByFiltering
        {
            get
            {
                return _ColumnNameModifiedByFiltering;
            }
            set
            {
                _ColumnNameModifiedByFiltering = value;
            }
        }

        private bool _ColumnNameModified = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("Modified"),
        WebDisplayName("Modified"),
        WebDescription("Modified")]
        public bool ColumnNameModified
        {
            get
            {
                return _ColumnNameModified;
            }
            set
            {
                _ColumnNameModified = value;
            }
        }

        private bool _ColumnNameModifiedFiltering = false;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("Modified (Filtering)"),
        WebDisplayName("Modified (Filtering)"),
        WebDescription("Modified (Filtering)")]
        public bool ColumnNameModifiedFiltering
        {
            get
            {
                return _ColumnNameModifiedFiltering;
            }
            set
            {
                _ColumnNameModifiedFiltering = value;
            }
        }

        private bool _ColumnNameFileSize = true;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("File Size"),
        WebDisplayName("File Size"),
        WebDescription("File Size")]
        public bool ColumnNameFileSize
        {
            get
            {
                return _ColumnNameFileSize;
            }
            set
            {
                _ColumnNameFileSize = value;
            }
        }

        private bool _ColumnNameFileSizeFiltering = false;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("Column Settings"),
        DisplayName("File Size (Filtering)"),
        WebDisplayName("File Size (Filtering)"),
        WebDescription("File Size (Filtering)")]
        public bool ColumnNameFileSizeFiltering
        {
            get
            {
                return _ColumnNameFileSizeFiltering;
            }
            set
            {
                _ColumnNameFileSizeFiltering = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            // No default styles
            if (this.Zone != null && this.Zone.WebPartChrome != null)
            {
                SPChromeSettings chromeSettings = ((SPChrome)this.Zone.WebPartChrome).GetSPChromeSettings(this);
                //chromeSettings.UseDefaultStyles = false;
                chromeSettings.UseDefaultStyles = false;
                //chromeSettings.OverflowAuto = true;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            //SPChromeSettings chromeSettings = ((SPChrome)this.Zone.WebPartChrome).GetSPChromeSettings(this);
            //chromeSettings.UseDefaultStyles = false;
            //chromeSettings.OverflowAuto = false;

            if (UseAccessibleHeader == false)
            {
                if (AllowFiltering == true)
                    AllowFiltering = false;
                if (AllowSorting == true)
                    AllowSorting = false;
            }

            if (ColumnNameFileName == false && ColumnNameFileNameFiltering == true)
            {
                ColumnNameFileNameFiltering = false;
            }
            if (ColumnNameFileSize == false && ColumnNameFileSizeFiltering == true)
            {
                ColumnNameFileSizeFiltering = false;
            }
            if (ColumnNameModified == false && ColumnNameModifiedFiltering == true)
            {
                ColumnNameModifiedFiltering = false;
            }
            if (ColumnNameModifiedBy == false && ColumnNameModifiedByFiltering == true)
            {
                ColumnNameModifiedByFiltering = false;
            }

            SiteDataQueryUserControl objSiteDataQueryUserControl = (SiteDataQueryUserControl)Page.LoadControl(_ascxPath);
            objSiteDataQueryUserControl._WebsScope = this.WebsScope;
            objSiteDataQueryUserControl._ListBaseType = this.ListBaseType;
            objSiteDataQueryUserControl._UseAccessibleHeader = this.UseAccessibleHeader;
            objSiteDataQueryUserControl._AllowFiltering = this.AllowFiltering;
            objSiteDataQueryUserControl._AllowPaging = this.AllowPaging;
            objSiteDataQueryUserControl._AllowSorting = this.AllowSorting;

            objSiteDataQueryUserControl._PageSize = this.PageSize;
            objSiteDataQueryUserControl._RowStyle_BackColor = this.RowStyle_BackColor;
            objSiteDataQueryUserControl._AlternatingRowStyle_BackColor = this.AlternatingRowStyle_BackColor;

            objSiteDataQueryUserControl._QueryRowLimit = this.QueryRowLimit;
            objSiteDataQueryUserControl._CAMLQuery = this.CAMLQuery;
            objSiteDataQueryUserControl._CAMLQueryOrderBy = this.CAMLQueryOrderBy;

            objSiteDataQueryUserControl._ColumnNameFileName = this.ColumnNameFileName;
            objSiteDataQueryUserControl._ColumnNameFileNameFiltering = this.ColumnNameFileNameFiltering;
            objSiteDataQueryUserControl._ColumnNameFileSize = this.ColumnNameFileSize;
            objSiteDataQueryUserControl._ColumnNameFileSizeFiltering = this.ColumnNameFileSizeFiltering;
            objSiteDataQueryUserControl._ColumnNameModified = this.ColumnNameModified;
            objSiteDataQueryUserControl._ColumnNameModifiedFiltering = this.ColumnNameModifiedFiltering;
            objSiteDataQueryUserControl._ColumnNameModifiedBy = this.ColumnNameModifiedBy;
            objSiteDataQueryUserControl._ColumnNameModifiedByFiltering = this.ColumnNameModifiedByFiltering;

            this.Controls.Add(objSiteDataQueryUserControl);
        }
    }
}
